package com.jerei.store.service.impl;

import com.jerei.store.entity.Address;
import com.jerei.store.entity.Order;
import com.jerei.store.entity.OrderItem;
import com.jerei.store.mapper.AddressMapper;
import com.jerei.store.mapper.OrderItemMapper;
import com.jerei.store.mapper.OrderMapper;
import com.jerei.store.service.IOrderService;
import com.jerei.store.service.ex.InsertException;
import com.jerei.store.service.ex.SelectException;
import com.jerei.store.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public Integer insertOrder(Integer aid, Long price,String username) {

        Address address=addressMapper.selectByAid(aid);
        if(address==null){
            throw new SelectException("地址不存在或者查询异常");
        }


        Order order=new Order();

        order.setUid(address.getUid());
        order.setRecvName(address.getReceiver());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        order.setPrice(price);
        Date date=new Date();
        order.setOrderTime(date);
        order.setStatus(0);
        order.setCreatedUser(username);
        order.setCreatedTime(date);
        order.setModifiedTime(date);
        order.setModifiedUser(username);

        Integer rows=orderMapper.insertOrder(order);
        if(rows!=1){
            throw new InsertException("创建订单失败");
        }
        return order.getOid();

    }

    @Override
    public Order selectByOid(Integer oid) {

        Order order=orderMapper.selectByOid(oid);
        if(order==null){
            throw new SelectException("查询订单失败");
        }
        return order;
    }

    @Override
    public void updateByOid(Integer oid, String username) {
        Date date=new Date();
        Integer rows= orderMapper.updateByOid(oid,username,date,date,1);
        if(rows!=1){
            throw new UpdateException("修改订单失败");
        }

    }

    @Override
    public List<Integer> findAllOid(Integer uid) {

        //List<Integer> list=orderMapper.selectByStatus(uid,1);


        return null;
    }


}
